package com.qf.controller;

import com.qf.model.entity.OutConsumption;
import com.qf.model.req.OutConsumptionAndPatientReq;
import com.qf.service.OutConsumptionService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * (OutConsumption)表控制层
 *
 * @author ZhiHuiJun
 * @since 2024-04-30 09:30:48
 */
@RestController
@RequestMapping("/api/outConsumption")
public class OutConsumptionController {
    /**
     * 服务对象
     */
    @Autowired
    private OutConsumptionService outConsumptionService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list/page")
    public R queryByPage(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize,
                         @RequestParam(required = false) String patientName,
                         @RequestParam(required = false) String orderNumber,
                         @RequestParam (required = false) String card) {
        return R.ok(outConsumptionService.selectConsumptionByCondition(pageNum,pageSize,patientName,orderNumber,card));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.outConsumptionService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param outConsumptionAndPatientReq 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add( OutConsumptionAndPatientReq outConsumptionAndPatientReq) {
           boolean result = outConsumptionService.insert(outConsumptionAndPatientReq);
           if (result){
               return R.ok();
           }
           return R.fail();
//        return R.ok(this.outConsumptionService.insert(outConsumption));
    }

    /**
     * 编辑数据
     *
     * @param outConsumption 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit( OutConsumption outConsumption) {
       int i = outConsumptionService.update(outConsumption);
        if (i>0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
       boolean flag = outConsumptionService.deleteById(id);
       if (flag){
           return R.ok();
       }
        return R.fail();
    }

    @RequestMapping("/del/batch")
    public R delBatch(@RequestParam List<Integer> list){
       boolean result = outConsumptionService.delBatch(list);
       if (result){
           return R.ok();
       }
       return R.fail();
    }

}

